package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.DeviceIotEnum;
import com.itlong.cloud.iot.service.base.IotBaseService;
import com.itlong.cloud.sms.SmsMsgService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 *  云电梯设备信息同步至IOT的队列消费者
 * @createDate 2018-03-14
 */
@Component
public class DeviceIotConsumer03 {
    private static final Logger LOG = LoggerFactory.getLogger(DeviceIotConsumer03.class);

    @Autowired
    IotBaseService iotBaseService;

    @Autowired
    SmsMsgService smsMsgService;

    /**
     * <desc>
     *     云设备信息同步至IOT消费端
     * </desc>
     * @param message
     * @param channel
     * @throws InterruptedException
     * @throws IOException
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_DEVICE_ELEVATOR_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException, IOException {
        byte[] body = message.getBody();
        try {
            boolean flag = false;
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JSON.parseObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            JSONObject js = JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent());
            String data = js.getString("data");
            LOG.info("【iot消费数据:】"+data);
            JSONObject datajs = JSONObject.parseObject(data);
            //如果消费的时间和生产的时间间隔时间长则报警
            if (datajs.containsKey("timestamp")) {
                long inTime = datajs.getLong("timestamp");
                long outTime = (new Date()).getTime();
                if (outTime - inTime >= 1000) {
                    LOG.error("【消费消息不及时】{} {}", outTime - inTime, data);
                    //smsMsgService.sendSmsCodeByEmail("xxfaxy@qq.com", "[消费消息不及时]" + data);
                    //smsMsgService.sendSmsCodeByEmail("zhouc@itlong.com.cn", "[消费消息不及时]" + data);
                }
            }
            String productKey = datajs.getString("productKey");
            Integer deviceOperateIotType = datajs.getInteger("deviceOperateIotType");
            if (deviceOperateIotType == DeviceIotEnum.REGIST_DEVICE.getCommandType()){
                flag = iotBaseService.registDevice(productKey,datajs.getString("deviceUnique"),datajs.getString("deviceType"));
            }else if (deviceOperateIotType == DeviceIotEnum.SAVE_DEVICE_PROP.getCommandType()){
//                flag = iotBaseService.saveDeviceProp(productKey,datajs.getString("deviceUnique"),datajs.getJSONObject("deviceProJs"));
            }else if (deviceOperateIotType == DeviceIotEnum.DELETE_DEVICE_PROP.getCommandType()){
//                flag = iotBaseService.deleteDeviceProp(productKey, datajs.getString("deviceUnique"), datajs.getJSONObject("deviceProJs"));
            }else if (deviceOperateIotType == DeviceIotEnum.BATCH_GET_DEVICE_STATE.getCommandType()){
                iotBaseService.batchGetDeviceState(productKey,datajs.getString("deviceUnique"));
            }else {
                LOG.info("【RabbitMQ_消费者3_云电梯设备信息同步至IOT消费端】云电梯设备业务类型指令错误");
            }
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            LOG.error("【RabbitMQ_消费者3_云电梯设备信息同步至IOT消费端】:解析异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }




}
